<?php

function yeepaySave($param) 
{
	include dirname(__FILE__).'/yeepayCommon.php';
	$p1_MerId	= $param['p1_MerId'];//商户编号
	if ($p1_MerId != $_GET['p1_MerId']) {
		return false;
	}
	$r0_Cmd		= $param['r0_Cmd'];//业务类型,固定值 ”Buy”.
	$r1_Code	= $param['r1_Code'];//支付结果,固定值 “1”, 代表支付成功.
	$r2_TrxId	= $param['r2_TrxId'];//易宝支付交易流水号,易宝支付平台产生的交易流水号，每笔订单唯一
	$r3_Amt		= $param['r3_Amt'];//支付金额,单位:元，精确到分.
	$r4_Cur		= $param['r4_Cur'];//交易币种,返回时是"RMB"
// 	$r5_Pid		= iconv('utf-8', 'gbk', $param['r5_Pid']);//商品名称,
	$r5_Pid		= $param['r5_Pid'];//商品名称,
	$r6_Order	= $param['r6_Order'];//商户订单号
	$r7_Uid		= $param['r7_Uid'];//易宝支付会员ID
	$r8_MP		= $param['r8_MP'];//商户扩展信息
	$r9_BType	= $param['r9_BType'];//交易结果返回类型, 为“1”: 浏览器重定向; 为“2”: 服务器点对点通讯.
	
	$channelOrderNo = $r2_TrxId;
	$orderNo = $r6_Order;
	
	$str = $p1_MerId.$r0_Cmd.$r1_Code.$r2_TrxId.$r3_Amt.$r4_Cur
			.$r5_Pid.$r6_Order.$r7_Uid.$r8_MP.$r9_BType;
	$hmac = HmacMd5($str, $merchantKey);
	
	if ($hmac != $param['hmac']) {
		return false;
	}
	
	// 验证通过开始处理
	$model = M('pay_order_log');
	$record = $model->where(array('order_no'=>$orderNo))->find();
	// 支付成功已处理,不需要再处理了
	if ($record['status'] == 2) {
		return true;
	}
	
	$now = time();
	
	$data = array(
		'status'			=> 2,// 先假设充值全部成功
		'pay_time'			=> $now,
		'end_time'			=> $now,
		'channel_order_no'	=> $channelOrderNo,
	);
	$return  = $model->where('id='.$record['id'])->data($data)->save();
	if (!$return) {
		return false;
	}
	
	// 调用充值到游戏的公用接口,使充值到游戏当中
	if ($record['target'] == 1) {
		$thinkphp = dirname(dirname(dirname(dirname(__FILE__)))).'/ThinkPHP/';
		include_once $thinkphp.'Extend/UserDefined/PayToGame.class.php';
		$return = PayToGame::pay($record);
		$payGameStatus = PayToGame::$pay_game_status;
		// 充值游戏成功了,按正常标志订单完成充值,否则只标志未充值到游戏,计划任务重新充值到游戏中去
		if ($return) {
			$status = 2;
		} else {
			$status = 1;
		}
		$data = array(
			'status'			=> $status,
			'pay_game_status'	=> $payGameStatus,
		);
		$return  = $model->where('id='.$record['id'])->data($data)->save();
// 		if (!$return) {
// 			return false;
// 		}
	} else {//充值到平台,只增加平台币,调用用户增加平台币接口
		$addCoin = $record['fact_value'];//添加的平台币
		$addCredit = $record['pay_money'] * C('PAY_GET_CREDIT_RATE');//添加的积分
		User::addCoin($record['uid'], $addCoin, 0, 0, $orderNo);
	}
	// 充值成功添加积分
	User::addCredit($record['uid'], $addCredit);
	
	// 最近充值游戏记录
	$where = 'uid='.$record['uid'].' and game_id='.$record['game_id'];
	$lastPay = M('last_pay_game_log');
	$lastRecord = $lastPay->where($where)->find();
	if ($lastRecord) {
		$data = array(
			'last_pay_time'	=> time()
		);
		$lastPay->where('id='.$lastRecord['id'])->data($data)->save();
	} else {
		$data = array(
			'uid'			=> $record['uid'],
			'game_id'		=> $record['game_id'],
			'last_pay_time'	=> time()
		);
		$lastPay->where('id='.$lastRecord['id'])->data($data)->add();
	}
	
	// 更新玩家充值信息字段
	$data = array(
		'last_pay_date'	=> strtotime('today'),
	);
	$userMo = M('user');
	$userMo->where(array('uid'=>$record['uid']))->save($data);
	$userMo->where(array('uid'=>$record['uid']))->setInc('pay_total', $record['fact_value']);
	
	return true;
}